﻿<%@ Page Language="VB" MasterPageFile="~/Default.master" Title="Edit News Article"
    ValidateRequest="false" %>

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="SubSonic" %>
<%@ Import Namespace="ClubStarterKit.Data" %>

<%@ Register TagPrefix="Club" TagName="LoginBanner" Src="~/UserControls/LoginBanner.ascx" %>
<%@ Register TagPrefix="Club" TagName="ImageThumbnail" Src="~/UserControls/ImageThumbnail.ascx" %>
<%@ Register TagPrefix="Club" TagName="Photopicker" Src="~/UserControls/photos_formpicker.ascx" %>
<%@ Register TagPrefix="Club" TagName="Dateandtimepicker" Src="~/UserControls/Dateandtimepicker.ascx" %>
<%@ Register TagPrefix="Club" TagName="PhotoAlbumPicker" Src="~/UserControls/photoalbum_picker.ascx" %>

<script runat="server">
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        If Not IsPostBack Then
            Dim action As Object = Request.QueryString("Action")
            If Not action Is Nothing Then
                If LCase(CStr(action)) = "new" Then
                    Response.Redirect("~/news/add.aspx")
                End If
                If LCase(CStr(action)) = "remove" Then
                    ClubStarterKit.Web.News.Delete(CStr(Request("ArticleID")))
                    Response.Redirect("list.aspx")
                End If
            End If
         
            Session("FCKeditor:UserFilesPath") = Request.ApplicationPath.ToString & "Files/"
        End If
    End Sub

    Protected Sub FormView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs)
        If FormView1.CurrentMode = FormViewMode.Insert Then
            CType(FormView1.FindControl("dtpicker"), DateandTimePicker).selectedDateTime = Now
        Else
            Dim cb As CheckBox = CType(FormView1.FindControl("CheckBox1"), CheckBox)
            Dim surl As TextBox = CType(FormView1.FindControl("staticURLTextBox"), TextBox)
            If surl.Text <> "" Then
                cb.Checked = True
                surl.Enabled = True
            Else
                surl.Enabled = False
            End If
          
        End If
    End Sub

    Protected Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim cb As CheckBox = CType(FormView1.FindControl("CheckBox1"), CheckBox)
        Dim surl As TextBox = CType(FormView1.FindControl("StaticURLTextBox"), TextBox)
        If cb.Checked Then
            surl.Enabled = True
        Else
            surl.Enabled = False
        End If
    End Sub

    Protected Sub apply1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim Title As String = CType(FormView1.FindControl("titleTextBox"), TextBox).Text.ToString()
        Dim URL As String = ""
        If CType(FormView1.FindControl("CheckBox1"), CheckBox).Checked = True Then
            URL = CType(FormView1.FindControl("staticURLTextBox"), TextBox).Text
        End If
        
        Dim description As String = "<p></p>"
        If Not CType(FormView1.FindControl("FCKeditor2"), FCKeditor).Value = "" Or IsDBNull(CType(FormView1.FindControl("FCKeditor2"), FCKeditor).Value) = False Then
            description = CType(FormView1.FindControl("FCKeditor2"), FCKeditor).Value
        End If
        
        Dim itemdate As Date = DateTime.Now()
        If IsDBNull(CType(FormView1.FindControl("dtpicker"), DateandTimePicker).selectedDateTime) = False Then
            itemdate = CType(FormView1.FindControl("dtpicker"), DateandTimePicker).selectedDateTime()
        End If
        
        Dim photoid As Integer = 0
        If IsDBNull(CType(FormView1.FindControl("Photopicker1"), Photos_formpicker).ImageId) = False Then
            photoid = CType(FormView1.FindControl("Photopicker1"), Photos_formpicker).ImageId
        End If
        
        Dim albumid As Integer = 0
        If IsDBNull(CType(FormView1.FindControl("albumpick"), AlbumPicker).AlbumID) = False Then
            albumid = Integer.Parse(CType(FormView1.FindControl("albumpick"), AlbumPicker).AlbumID.ToString())
        End If
        
        Dim upqry As New Query(Tables.Announcements)
        upqry.QueryType = QueryType.Update
        upqry.AddWhere(Announcements.Columns.Id, CStr(Request("ArticleID")))
        upqry.AddUpdateSetting(Announcements.Columns.Albumid, albumid)
        upqry.AddUpdateSetting(Announcements.Columns.Description, description)
        upqry.AddUpdateSetting(Announcements.Columns.Itemdate, itemdate)
        upqry.AddUpdateSetting(Announcements.Columns.Photo, photoid)
        upqry.AddUpdateSetting(Announcements.Columns.StaticURL, URL)
        upqry.AddUpdateSetting(Announcements.Columns.Title, Title)
        upqry.Execute()
        Response.Redirect("~/news/view.aspx?articleid=" & CStr(Request("ArticleID")))
    End Sub
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <div id="body">
        <Club:LoginBanner ID="LoginBanner1" runat="server" />
        <asp:FormView ID="FormView1" runat="server" DataSourceID="ArticleOBJDS" DataKeyNames="ID"
            DefaultMode="Edit" OnDataBound="FormView1_DataBound">
            <EditItemTemplate>
                <div class="fullwidth">
                    <h3>
                        Edit News Article</h3>
                    <div class="dashedline">
                    </div>
                    <table>
                        <tr>
                            <td class="formlabel">
                                Article Title:
                            </td>
                            <td align="left">
                                <asp:TextBox ID="titleTextBox" runat="server" Width="500px" Text='<%# Bind("title") %>'></asp:TextBox><br />
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="titleTextBox"
                                    ErrorMessage="Please enter a title." ToolTip="Please enter a title." ValidationGroup="edit">*</asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td class="formlabel">
                                Link:
                            </td>
                            <td align="left">
                                <asp:CheckBox ID="CheckBox1" runat="server" Text="Use a link instead of inline content for this article."
                                    OnCheckedChanged="CheckBox1_CheckedChanged" AutoPostBack="true" /><br />
                                URL:
                                <asp:TextBox Text='<%# Bind("staticURL") %>' runat="server" ID="staticURLTextBox"
                                    Width="450px" />
                            </td>
                        </tr>
                        <tr>
                            <td class="formlabel">
                                <asp:Label ID="Label1" runat="server" Text="Description:" />
                            </td>
                            <td align="left">
                            <FCKeditorV2:FCKeditor ID="FCKeditor2" runat="server" ToolbarSet = "Default" SkinPath = "skins/office2003/" Value='<%# Bind("description") %>' BasePath="~/FCKeditor/" UseBROnCarriageReturn="true"></FCKeditorV2:FCKeditor>
                                <br />
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="FCKeditor2"
                                    EnableClientScript="False" ErrorMessage="Please enter a description." ToolTip="Please enter a description."
                                    ValidationGroup="edit">*</asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td class="formlabel">
                                Article Visible Date</td>
                            <td align="left">
                                <p>
                                    The news article will not be visible to users until after this date.
                                </p>
                                <Club:Dateandtimepicker ID="dtpicker" runat="server" selectedDateTime='<%#Bind("itemdate") %>' />
                            </td>
                        </tr>
                        <tr>
                            <td class="formlabel">
                                Photo:</td>
                            <td align="left">
                                <Club:Photopicker ID="Photopicker1" runat="server" ImageId='<%# Bind("photo") %>'
                                    SmallImage="true" />
                            </td>
                        </tr>
                        <tr>
                            <td class="formlabel">
                                Photo Album</td>
                            <td align="left">
                                <p>
                                    Optionally, the news article can be assoiated with a photo album.
                                </p>
                                <Club:PhotoAlbumPicker ID="albumpick" runat="server" AlbumID='<%# bind("Albumid") %>' />
                            </td>
                        </tr>
                    </table>
                    <div class="dashedline">
                    </div>
                    <div class="actionbuttons">
                        <Club:RolloverButton ID="apply1" CausesValidation="true" Text="Save" runat="server" ValidationGroup="edit" OnClick="apply1_Click" />
                        <Club:RolloverLink ID="Cancel" CausesValidation="false" Text="Cancel" runat="server" NavigateURL='<%# "view.aspx?ArticleID=" & Cstr(Eval("ID")) %>' />
                    </div>
                </div>
                <div>
                </div>
            </EditItemTemplate>
        </asp:FormView>
    </div>
    
    <asp:ObjectDataSource ID="ArticleOBJDS" runat="server" SelectMethod="FetchByParameter" TypeName="ClubStarterKit.Data.Announcements">
        <SelectParameters>
            <asp:Parameter DefaultValue="ID" Name="columnName" Type="String" />
            <asp:QueryStringParameter DefaultValue="" Name="oValue" QueryStringField="ArticleID"
                Type="Object" />
        </SelectParameters>
    </asp:ObjectDataSource>
</asp:Content>
